/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *  2011-03-25 Marco Riccardi - implementation of visitor pattern
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLElement;

public abstract class CorrectnessCondition implements INode {

    private enum MySubclass { UnknownCorrCond, Coherence, Compatibility, Consistency, Existence, Uniqueness,}

    public static boolean isCorrectnessCondition(String className){
        boolean found = false;
        for (MySubclass mySubclass :MySubclass.values()){
            if (mySubclass.name().equals(className)){
                found = true;
                break;
            }
        }
        return found;
    }

    public static CorrectnessCondition newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        XMLElement xmlElement = xmlElementList.peek();
        if (!isCorrectnessCondition(xmlElement.getName())){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a subclass of CorrectnessCondition", xmlElementList);
        }
        MySubclass mySubclass = MySubclass.valueOf(xmlElement.getName());
        switch (mySubclass) {
        case UnknownCorrCond: return UnknownCorrCond.newInstance(parentNode, xmlElementList);
        case Coherence: return Coherence.newInstance(parentNode, xmlElementList);
        case Compatibility: return Compatibility.newInstance(parentNode, xmlElementList);
        case Consistency: return Consistency.newInstance(parentNode, xmlElementList);
        case Existence: return Existence.newInstance(parentNode, xmlElementList);
        case Uniqueness: return Uniqueness.newInstance(parentNode, xmlElementList);
        default: throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a subclass of CorrectnessCondition", xmlElementList);
        }
    }

    public LinkedList<XMLElement> getXMLElementList() {
        if (this instanceof UnknownCorrCond) {
            return ((UnknownCorrCond) this).getXMLElementList();
        } else 
        if (this instanceof Coherence) {
            return ((Coherence) this).getXMLElementList();
        } else 
        if (this instanceof Compatibility) {
            return ((Compatibility) this).getXMLElementList();
        } else 
        if (this instanceof Consistency) {
            return ((Consistency) this).getXMLElementList();
        } else 
        if (this instanceof Existence) {
            return ((Existence) this).getXMLElementList();
        } else 
        if (this instanceof Uniqueness) {
            return ((Uniqueness) this).getXMLElementList();
        } else 
        return new LinkedList<XMLElement>();
    }
}
